﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ComFun;
using ComLib.ComType;
using ComLib.ConfigService;
using System.Drawing.Imaging;

namespace ProcessConfig.ConfigType
{
    [Serializable]
    [XmlRootAttribute("PhotoAdjustOptions", Namespace = "ProcessConfig.ConfigType", IsNullable = false)]
    public class PxlAdjustOptions : XmlSerializeBase
    {
        public PxlAdjustOptions()
        {
            _options = new XmlDictionary<ApplyTo, PxlAdjustOption>();
            _options.Add(ApplyTo.ExitAndEntry, new PxlAdjustOption());
            _options.Add(ApplyTo.IDCard, new PxlAdjustOption());
            _options.Add(ApplyTo.PortVisa, new PxlAdjustOption());
            _options.Add(ApplyTo.SocialSecurity, new PxlAdjustOption());
            _options.Add(ApplyTo.DrivingLicence, new PxlAdjustOption());
        }

        [XmlIgnore]
        public PxlAdjustOption this[ApplyTo app]
        {
            get
            {
                if (_options.ContainsKey(app))
                {
                    return _options[app];
                }
                else
                    return null;
            }
            set
            {
                if (!_options.ContainsKey(app))
                {
                    _options.Add(app, value);
                }
                else
                {
                    _options[app] = value;
                }
            }
        }

        /// <summary>
        /// 选项列表
        /// </summary>
        private XmlDictionary<ApplyTo, PxlAdjustOption> _options;
        public XmlDictionary<ApplyTo, PxlAdjustOption> Options
        {
            get { return _options; }
            set { _options = value; }
        }


        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            PxlAdjustOptions obj = (PxlAdjustOptions)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this._nVer = obj._nVer;
                this._options = obj._options;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("PXL调整选项: ");
            FillFieldValueStringDelegate FillString =
                (string field, object val) =>
                {
                    if(val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }                  
                };
            FillString("出入境", _options[ApplyTo.ExitAndEntry]);
            FillString("身份证", _options[ApplyTo.IDCard]);
            FillString("社保", _options[ApplyTo.PortVisa]);
            FillString("口岸签证", _options[ApplyTo.SocialSecurity]);
            FillString("驾驶证", _options[ApplyTo.DrivingLicence]);
            return builder.ToString();
        }
    }

    public enum AdjustParams : int
    {
        //自动调整选项
        CROP_IMAGE = 0x00000001,		// 裁切
        CLEAN_BKCOLOR = 0x00000002,	    // 清理背景
        ADJUST_FACECOLOR = 0x00000004,      // 调整面部颜色
        CLEAN_HOTS = 0x00000008,		// 清理高光
        FORCE_CROP = 0x00010000,		// 强制剪裁（否则不够裁时自动调整会直接返回错误_FGE_IMAGECROPSCALELACK）
        CLEAN_BKCOLOR_EX = 0x00000010,  // 增强清理背景
    };

    public class PxlAdjustOption
    {
        private bool _bCrop = true;
        public bool CropImage
        {
            get { return _bCrop; }
            set { _bCrop = value; }
        }

        private bool _bCleanBKC = true;
        public bool CleanBKC
        {
            get { return _bCleanBKC; }
            set { _bCleanBKC = value; }
        }

        private bool _bCleanBKEX = true;
        public bool CleanBKEX
        {
            get { return _bCleanBKEX; }
            set { _bCleanBKEX = value; }
        }

        private bool _bAdjustFaceColor = true;
        public bool AdjustFaceColor
        {
            get { return _bAdjustFaceColor; }
            set { _bAdjustFaceColor = value; }
        }

        private bool _bCleanHots = false;
        public bool CleanHots
        {
            get { return _bCleanHots; }
            set { _bCleanHots = value; }
        }

        private bool _bForceCropImage = false;
        public bool ForceCropImage
        {
            get { return _bForceCropImage; }
            set { _bForceCropImage = value; }
        }

        /// <summary>
        /// 应用评价略略
        /// </summary>
        private bool _bApplyAssessPolicies = true;
        public bool ApplyAssessPolicies
        {
            get { return _bApplyAssessPolicies; }
            set { _bApplyAssessPolicies = value; }
        }
     
        /// <summary>
        /// 评价略略选项
        /// </summary>
        private DetectAssessPolicies _assessPolicies = new DetectAssessPolicies();
        public DetectAssessPolicies AssessPolicies
        {
            get { return _assessPolicies; }
            set { _assessPolicies = value; }
        }

        /// <summary>
        /// 像素裁切选项组合枚举值
        /// </summary>
        public int AdjustFlag
        {
            get
            {
                int val = 0;
                val |= (_bCrop ? (int)AdjustParams.CROP_IMAGE : 0);
                val |= (_bCleanBKC ? (int)AdjustParams.CLEAN_BKCOLOR : 0);
                val |= (_bAdjustFaceColor ? (int)AdjustParams.ADJUST_FACECOLOR : 0);
                val |= (_bCleanHots ? (int)AdjustParams.CLEAN_HOTS : 0);
                val |= (_bForceCropImage ? (int)AdjustParams.FORCE_CROP : 0);
                val |= (_bCleanBKEX ? (int)AdjustParams.CLEAN_BKCOLOR_EX : 0);
                return val;
            }

        }
        public override string ToString()
        {
            StringBuilder strEnabled = new StringBuilder("PXL裁剪和调整选项: ");
            StringBuilder strDisabled = new StringBuilder();
            FillEnabledStringDelegate FillString =
                (string baseStr, bool enabled) =>
                {
                    if (enabled)
                    {
                        strEnabled.Append("[");
                        strEnabled.Append(baseStr);
                        strEnabled.Append("]");
                        strEnabled.Append("已启用; ");
                    }
                    else
                    {
                        strDisabled.Append("[");
                        strDisabled.Append(baseStr);
                        strDisabled.Append("]");
                        strDisabled.Append("未启用; ");
                    }
                };

            FillString("裁剪", _bCrop);
            FillString("清理背景", _bCleanBKC);
            FillString("调整脸部颜色", _bAdjustFaceColor);
            FillString("清理高光", _bCleanHots);
            FillString("强制裁剪", _bForceCropImage);
            FillString("增强清理背景", _bCleanBKEX);
            StringBuilder builder = new StringBuilder();       
            strEnabled.Append(strDisabled.ToString());
            strEnabled.Append(builder.ToString());
            return strEnabled.ToString();
        }
    }
}
